package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version v1.0
 * @package com.itheima.controller
 * @description:
 * @author: Ewaits
 * @date: 2019/7/2 21:33
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @RequestMapping("getUsername")
    public Result getUsername() {
        try {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }

    }

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("add")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        try {
            String encode = passwordEncoder.encode(user.getPassword());
            user.setPassword(encode);
            userService.add(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加用户失败");
        }

        return new Result(true, "添加用户成功");
    }


    @RequestMapping("deleteUserById")
    public Result deleteUserById(Integer id) {
        try {
            userService.deleteUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除用户失败");
        }
        return new Result(true, "删除用户成功");
    }

    @RequestMapping("findUserById")
    public Result findUserById(Integer id) {
        try {
            User user = userService.findUserById(id);
            user.setPassword(null);
            if (ObjectUtils.isEmpty(user)) {
                return new Result(false, "用户不存在");
            }
            return new Result(true, "查询用户成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询用户失败");
        }
    }

    @RequestMapping("findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id) {
        try {
            Integer[] roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true, "查询角色id成功", roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色id失败");
        }

    }

    @RequestMapping("edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            if (!StringUtils.isEmpty(user.getPassword())) {
                String encode = passwordEncoder.encode(user.getPassword());
                user.setPassword(encode);
            }
            userService.edit(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑用户失败");
        }
        return new Result(true, "编辑用户成功");
    }

    @RequestMapping("getLoginUserId")
    public Result getLoginUserId() {
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        User loginUser = userService.findUserByUsername(username);
        return new Result(true, "获取登录用户id成功", loginUser.getId());
    }



}
